<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>服务分配--CRM客户关系管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" placeholder="请输入客户姓名"/>
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
            </div>
        </form>
    </blockquote>
    <table id="serviceDistribute" lay-filter="serviceDistribute"></table>
    <!--操作-->
    <script type="text/html" id="serviceDistributeBar">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="assignPeople">分配</a>
    </script>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['form', 'layer', 'table', 'laytpl'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table;
        //用户列表
        var tableIns = table.render({
            elem: '#serviceDistribute',
            url: 'services.do',
            cellMinWidth: 95,
            page: true,
            height: "full-200",
            limits: [10, 15, 20, 25],
            limit: 30,
            id: "serviceDistributeTable",
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'id', title: '编号', minWidth: 100, align: "center"},
                {field: 'client', title: '客户', minWidth: 100, align: "center"},
                {field: 'type', title: '服务类型', align: 'center'},
                {field: 'founder', title: '创建人', align: 'center'},
                {
                    field: 'creatTime', title: '创建日期', align: 'center', minWidth: 90, templet: function (obj) {
                        return obj.creatTime;
                    }
                },
                {field: 'outline', title: '概要', align: 'center'},
                {title: '操作', width: 260, templet: '#serviceDistributeBar', fixed: "right", align: "center"}
            ]]
        });
        //实现搜索功能
        $(".search_btn").click(function () {
            var name = $(".searchVal").val();
            table.reload('serviceDistributeTable', {url: "services.do?client=" + name});
        });


        // tool(xx)这里的xx是和上边表个的lay-filter="userList"对应的
        table.on("tool(serviceDistribute)", function (obj) {
            //获取当前行的数据
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    deleteService(data);
                    break;
                case 'assignPeople':
                    selectPeople(data);
                    break;
            }
        });

        //删除方法
        function deleteService(data) {
            layer.confirm('您确定要删除【' + data.client + '】这个用户吗？', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    url: "service/deleteService.do",
                    data: {id: data.id},
                    type: "get",
                    dataType: "json",
                    success: function (res) {
                        console.log(res + "****")
                        if (res.code == 200) {
                            //刷新表格
                            tableIns.reload();
                        } else if (res.code == -1) {
                            layer.msg(res.msg);
                        }
                    }
                });
                //关闭提示框
                layer.close(index);
            });
        }

        //批量删除
        $(".delAll_btn").click(function () {
            var checkStatus = table.checkStatus('serviceDistributeTable');
            var data = checkStatus.data;
            var uIds = [];
            if (data.length > 0) {
                for (var i in data) {
                    uIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: "service/deleteBatchService.do",
                        type: "GET",
                        data: {ids: uIds},
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 200) {
                                tableIns.reload();
                                layer.close(index);
                            }
                        }
                    });
                })
            } else {
                layer.msg("请选择需要删除的用户");
            }
        });

        //开发分配角色的弹出框
        function selectPeople(data) {
            var assignPeopleIndex = layer.open({
                type: 2,
                title: "分配人员",
                content: "toAssignPeople.do",
                area: ['650px', '350px'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                    body.contents().find("#serviceIdHiden").val(data.id);//这样就将值传递给子页面中id为serviceIdHiden的隐藏域
                }
            });
            window.sessionStorage.setItem("assignPeopleIndex", assignPeopleIndex);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        }
    });

</script>
</body>
</html>